//
//  ZXBoutiqueViewController.m
//  XiMaFM
//
//  Created by fanzhuxian on 2017/9/6.
//  Copyright © 2017年 hcios. All rights reserved.
//

#import "ZXBoutiqueViewController.h"

#import "ZXBotuiqueSpecialViewController.h"
#import "ZXDestinationViewViewController.h"

#import "ZXBoutiqueViewController.h"
#import "ZXBoutiqueCollectionViewCell.h"

#import "ZXBoutiqueViewModel.h"

#import "CXRefreshStateHeader.h"

#import "ZXTitleView.h"

#import <SDCycleScrollView.h>

static NSString * const boutiqueCell = @"BotiqueCell";
static NSString * const hCell = @"UICollectionViewHeader";
static NSString * const nCell = @"newHeadView";


@interface ZXBoutiqueViewController ()<UICollectionViewDelegate, UICollectionViewDataSource,SDCycleScrollViewDelegate, ZXTitleViewDelegate>

@property (nonatomic, strong) UICollectionView * collectionView;

@property (nonatomic, strong) ZXBoutiqueViewModel * boutiqueModel;

@property(nonatomic,strong) NSString * type;

@property(nonatomic,assign) NSInteger categoryID;

@end

@implementation ZXBoutiqueViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    [self.collectionView.mj_header beginRefreshing];
}

#pragma mark - headView 创建头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        UICollectionReusableView * headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:hCell forIndexPath:indexPath];
        
        NSMutableArray * imageUrl = [[NSMutableArray alloc] init];
        
        for (NSInteger i = 0; i < self.boutiqueModel.focusImgNumber; i++) {
            
            NSString * str = [NSString stringWithFormat:@"%@", [self.boutiqueModel focusImgURLForIndex:i]];
            [imageUrl addObject:str];
            
            
            NSLog(@"%@", str);
        }

        SDCycleScrollView * scrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, DEVICE_SCREEN_WIDTH, DEVICE_SCREEN_HEIGHT / 4) imageNamesGroup:imageUrl];
        
        scrollView.layer.masksToBounds = YES;
        scrollView.layer.cornerRadius = 4;
        scrollView.delegate = self;
        scrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
        scrollView.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        [headerView addSubview:scrollView];
        
        return headerView;

    } else {
    
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:nCell forIndexPath:indexPath];
        
        ZXTitleView * moreView = [[ZXTitleView alloc] initWithTitle:[self.boutiqueModel mainTitleForSection:indexPath.section] hasMore:[self.boutiqueModel hasMoreForSection:indexPath.section]];
        
        moreView.delegate = self;
        moreView.tag = indexPath.section;
        moreView.frame = headerView.bounds;
        [headerView addSubview:moreView];
        
        return headerView;
    }
    
}

#pragma mark - ZXTitleView Delegate
- (void)titleViewDidClick:(NSInteger)tag {

    _categoryID = [self.boutiqueModel categoryIdForSection:tag];
    _type = [self.boutiqueModel contentTypeForSection:tag];
    
    ZXBotuiqueSpecialViewController * specialVC = [[ZXBotuiqueSpecialViewController alloc] init];
    specialVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:specialVC animated:YES];
    
    
}

#pragma mark - 图片点击事件
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    
    NSLog(@"%ld",(long)index);
}

// 设置头视图大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
referenceSizeForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        return CGSizeMake(DEVICE_SCREEN_WIDTH, DEVICE_SCREEN_HEIGHT / 4);
        
    } else if (section < 5) {
    
        return  CGSizeMake(0, 0);
    } else {
    
        return CGSizeMake(0, 40);
    }
        
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(DEVICE_SCREEN_WIDTH - 20, 100);
    
}

#pragma mark - collection DataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return self.boutiqueModel.section;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    if (section < 5) {
        return 0;
    }
    return [self.boutiqueModel categoryContentsCount:section];
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    ZXBoutiqueCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:boutiqueCell forIndexPath:indexPath];
    
    [cell.icoImgView sd_setImageWithURL:[self.boutiqueModel coverURLForSection:indexPath.section index:indexPath.item] placeholderImage:[UIImage imageNamed:@"cell_bg_noData_2"]];

    cell.titleLabel.text = [self.boutiqueModel titleForSection:indexPath.section index:indexPath.item];
   
    cell.commentsCountLabel.text = [NSString stringWithFormat:@"%ld", [self.boutiqueModel commentsCountForSection:indexPath.section index:indexPath.item]];
    
    cell.priceLabel.text = [NSString stringWithFormat:@"%ld", [self.boutiqueModel priceForSection:indexPath.section index:indexPath.item]];
   
    cell.displayPriceLabel.text = [self.boutiqueModel displayVipPriceForSection:indexPath.section index:indexPath.item];
    
    return cell;
     
}

#pragma mark - collectionViewCell 点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"%ld", indexPath.section);
    NSLog(@"%ld", indexPath.row);
    
    ZXDestinationViewViewController * destiationVC = [[ZXDestinationViewViewController alloc] initWithAlumId:[self.boutiqueModel albumIdForSection:indexPath.section index:indexPath.item] title:[self.boutiqueModel mainTitleForSection:indexPath.section]];
    [self.navigationController pushViewController:destiationVC animated:YES];
}

- (UICollectionView *)collectionView {

    if (!_collectionView) {
        UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumLineSpacing = 1.0f;
        flowLayout.minimumInteritemSpacing = 1;
        flowLayout.itemSize = CGSizeMake(DEVICE_SCREEN_WIDTH, 125);
        flowLayout.headerReferenceSize = CGSizeMake(DEVICE_SCREEN_WIDTH, DEVICE_SCREEN_HEIGHT / 4);
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 3, 10);
        
        UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, DEVICE_SCREEN_WIDTH, DEVICE_SCREEN_HEIGHT - 35 - 49 - 64) collectionViewLayout:flowLayout];
        [self.view addSubview:collectionView];
        
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.backgroundColor =  [UIColor colorWithRed:239 / 255.0  green:243 / 255.0  blue:247 / 255.0 alpha:1];
        
        [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([ZXBoutiqueCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:boutiqueCell];
        
        collectionView.bounces = YES;
        
        // 注册头视图
        [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:hCell];
        
        [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:nCell];
    
        self.collectionView = collectionView;
        
        // 上拉刷新
        collectionView.mj_header = [CXRefreshStateHeader headerWithRefreshingBlock:^{
            [self.boutiqueModel getDataCompletionHandle:^(NSError *error) {
                [self.collectionView.mj_header endRefreshing];
                [self.collectionView reloadData];
            }];
        }];
        
    }
    return _collectionView;
}

#pragma mark - 懒加载
- (ZXBoutiqueViewModel *)boutiqueModel {

    if (!_boutiqueModel) {
        _boutiqueModel = [[ZXBoutiqueViewModel alloc] init];
    }
    return _boutiqueModel;
}

@end
